package com.app.couponsharelife.ui.adapter

import androidx.databinding.DataBindingUtil
import android.view.LayoutInflater
import android.view.ViewGroup
import com.app.couponsharelife.R
import com.app.couponsharelife.data.bean.PageTipBean
import com.app.couponsharelife.data.bean.SecondarySortBean
import com.app.couponsharelife.databinding.SortDetailItemBinding
import com.app.couponsharelife.ui.listener.OnClickListener

/**
 * Created by guluwa on 2018/11/23.
 */
class SortDetailAdapter(private var list: MutableList<Any>, private val listener: OnClickListener) :
        androidx.recyclerview.widget.RecyclerView.Adapter<androidx.recyclerview.widget.RecyclerView.ViewHolder>() {

    fun setData(data: Any) {
        list.clear()
        list.add(data)
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): androidx.recyclerview.widget.RecyclerView.ViewHolder {
        return if (viewType == 1) ViewHolder(DataBindingUtil.inflate(
                LayoutInflater.from(parent.context), R.layout.sort_detail_item, parent, false))
        else ViewHolderTip(DataBindingUtil.inflate(
                LayoutInflater.from(parent.context), R.layout.page_list_tip_item, parent, false))
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun getItemViewType(position: Int): Int {
        return if (list[position] is PageTipBean) 0 else 1
    }

    override fun onBindViewHolder(holder: androidx.recyclerview.widget.RecyclerView.ViewHolder, position: Int) {
        if (getItemViewType(position) == 0) {
            (holder as ViewHolderTip).databinding.pageTipBean = list[position] as PageTipBean
        } else {
            val secondarySortBean = list[position] as SecondarySortBean
            (holder as ViewHolder).databinding.tvSort.text = secondarySortBean.name
            holder.databinding.mRecyclerView.layoutManager = androidx.recyclerview.widget.GridLayoutManager(holder.itemView.context, 3)
            holder.databinding.mRecyclerView.adapter = SortDetailListAdapter(secondarySortBean.list, object : OnClickListener {
                override fun click(arg1: Int, arg2: Any) {
                    listener.click(0, arg2)
                }
            })
        }
    }

    inner class ViewHolder(val databinding: SortDetailItemBinding) : androidx.recyclerview.widget.RecyclerView.ViewHolder(databinding.root)
}